import { IPlank } from '@/types';
import { useMemo } from 'react';

interface Data {
  plankData: IPlank;
  holeId: string;
  grooveId: string;
  isUseCutting: boolean;
}

type PartialData = Partial<Data>;

type Props = PartialData;

export function usePlankProps(props: Props) {
  const { plankData, holeId, grooveId, isUseCutting } = props;

  const plankProps = useMemo(() => {
    return (
      plankData && getPlankProps({ plankData, holeId, grooveId, isUseCutting })
    );
  }, [plankData, holeId, grooveId, isUseCutting]);

  return [plankProps];
}

function getPlankProps(data: PartialData) {
  const { plankData, holeId, grooveId, isUseCutting } = data;

  if (!plankData) return;

  const {
    finishedProfile,
    // @ts-ignore
    cuttingProfile,
    finishedWidth,
    // @ts-ignore
    cuttingWidth,
    finishedHeight,
    // @ts-ignore
    cuttingHeight,
    holes,
    thickness,
    grooves,
    edgeBanding,
    // @ts-ignore
    textureDirection,
  } = plankData;

  const profile = isUseCutting ? cuttingProfile : finishedProfile;
  const width = isUseCutting ? cuttingWidth : finishedWidth;
  const height = isUseCutting ? cuttingHeight : finishedHeight;
  const hightLight = {
    holes: holeId ? [holeId] : [],
    grooves: grooveId ? [grooveId] : [],
  };

  return {
    ...plankData,
    profile,
    width,
    height,
    holes,
    thickness,
    grooves,
    hightLight,
    edgeBanding,
    textureDirection,
  };
}
